//
//  ViewController.m
//  iOSInterview
//
//  Created by macbook on 2025/5/29.
//

#import "ViewController.h"
#import "Masonry.h"
#import "AWHttpService.h"
#import "SVProgressHUD.h"
#import "NSDictionary+JSON.h"

#define SUCCESS [[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"status"]] integerValue] == 200

@interface ViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *enterPhoneTF;
@property (nonatomic, strong) UILabel *showResponseLabel;
@property (nonatomic, strong) UIButton *requestButton;
@property (nonatomic, strong) UIButton *faildRequestButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createUI];
}

- (void)createUI {
    [self.view addSubview:self.enterPhoneTF];
    [self.view addSubview:self.requestButton];
    [self.view addSubview:self.faildRequestButton];
    [self.view addSubview:self.showResponseLabel];

    [self.enterPhoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(60);
        make.left.equalTo(self.view.mas_left).offset(50);
        make.right.equalTo(self.view.mas_right).offset(-50);
        make.height.mas_equalTo(50);
    }];
    
    [self.requestButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.enterPhoneTF.mas_bottom).offset(20);
        make.left.equalTo(self.view.mas_left).offset(50);
        make.right.equalTo(self.view.mas_right).offset(-50);
        make.height.mas_equalTo(50);
    }];
    
    [self.faildRequestButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.requestButton.mas_bottom).offset(10);
        make.left.equalTo(self.view.mas_left).offset(50);
        make.right.equalTo(self.view.mas_right).offset(-50);
        make.height.mas_equalTo(50);
    }];
    
    [self.showResponseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.faildRequestButton.mas_bottom).offset(20);
        make.left.equalTo(self.view.mas_left).offset(50);
        make.right.equalTo(self.view.mas_right).offset(-50);
        make.height.mas_equalTo(200);
    }];
}

- (void)requestButtonClick {
    NSLog(@"发送请求");
    
    [self loadRequestWithFaildIs:NO];
}

- (void)faildRequestButtonClick {
    NSLog(@"发送错误请求");
    [self loadRequestWithFaildIs:YES];
}

#pragma mark - Request
- (void)loadRequestWithFaildIs:(BOOL)faildIs {
    if (self.enterPhoneTF.text.length == 0 || self.enterPhoneTF.text.length < 10) {
        [SVProgressHUD showErrorWithStatus:@"请输入手机号"];
        return;
    }
    
    NSDictionary *params = faildIs?@{@"type":@"text",@"mobile":@"1234567890"}:@{@"type":@"text",@"mobile":self.enterPhoneTF.text};
    [[AWHttpService service] postWithUrl:@"auth/v3.1/user/sendVerifiyCode" params:params succ:^(id  _Nonnull responseObject) {
        if (SUCCESS) {
            NSLog(@"请求结果>>>>>>>>>>>>>>>>>>>>>>\n\n%@",[NSDictionary jsonStringWithDictionary:responseObject]);
            self.showResponseLabel.text = [NSDictionary jsonStringWithDictionary:responseObject];
        }else {
            NSLog(@"请求结果>>>>>>>>>>>>>>>>>>>>>>\n\n%@",[NSDictionary jsonStringWithDictionary:responseObject]);
            NSString *message = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"message"]];
            [SVProgressHUD showErrorWithStatus:message];
        }
    } fail:^(NSInteger errorType, id  _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"请求失败"];
    }];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (newString.length == 0) {
        return YES;
    }
    
    if (newString.length > 10) {
        return NO;
    }
    
    NSCharacterSet *nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    if ([newString rangeOfCharacterFromSet:nonDigits].location != NSNotFound) {
        return NO;
    }
    
    if (newString.length == 1) {
        return [newString isEqualToString:@"5"];
    } else {
        return [[newString substringToIndex:2] isEqualToString:@"55"];
    }
}

#pragma LazyLoad
- (UIButton *)requestButton {
    if (!_requestButton) {
        _requestButton = [[UIButton alloc] init];
        _requestButton.backgroundColor = [UIColor greenColor];
        _requestButton.clipsToBounds = YES;
        _requestButton.layer.cornerRadius = 6.0f;
        [_requestButton setTitle:@"发送请求" forState:0];
        [_requestButton addTarget:self action:@selector(requestButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _requestButton;
}

- (UIButton *)faildRequestButton {
    if (!_faildRequestButton) {
        _faildRequestButton = [[UIButton alloc] init];
        _faildRequestButton.backgroundColor = [UIColor redColor];
        _faildRequestButton.clipsToBounds = YES;
        _faildRequestButton.layer.cornerRadius = 6.0f;
        [_faildRequestButton setTitle:@"发送失败的请求" forState:0];
        [_faildRequestButton addTarget:self action:@selector(faildRequestButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _faildRequestButton;
}

- (UILabel *)showResponseLabel {
    if (!_showResponseLabel) {
        _showResponseLabel = [[UILabel alloc] init];
        _showResponseLabel.numberOfLines = 0;
        _showResponseLabel.backgroundColor = [UIColor lightGrayColor];
        _showResponseLabel.clipsToBounds = YES;
        _showResponseLabel.layer.cornerRadius = 6.0f;
    }
    return _showResponseLabel;
}

- (UITextField *)enterPhoneTF {
    if (!_enterPhoneTF) {
        _enterPhoneTF = [[UITextField alloc] init];
        _enterPhoneTF.backgroundColor = [UIColor whiteColor];
        _enterPhoneTF.clipsToBounds = YES;
        _enterPhoneTF.layer.cornerRadius = 6.0f;
        _enterPhoneTF.layer.borderWidth = 0.5f;
        _enterPhoneTF.layer.borderColor = [UIColor blackColor].CGColor;
        _enterPhoneTF.delegate = self;
        _enterPhoneTF.borderStyle = UITextBorderStyleRoundedRect;
        _enterPhoneTF.placeholder = @"55XXXXXXXX";
        _enterPhoneTF.keyboardType = UIKeyboardTypeNumberPad; // 数字键盘
        _enterPhoneTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _enterPhoneTF.autocorrectionType = UITextAutocorrectionTypeNo;
    }
    return _enterPhoneTF;
}
@end
